﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeDynamiksEngine;
using MESCodeFile;

namespace mes_tublay
{
    public partial class frmLineofBusiness : Form
    {
        //Exclusive to enable the fully threaded mode of the CodeProcessor.
        //Yields faster time on multi-core processors but stalls greatly with single-cores.
        CodeProcessor proc = new CodeProcessor(ThreadingMode.Exclusive);
        CodeScript script;
        DataCache dat = new DataCache("MyData");
        AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
        int x;
        string[] blist=new string[0];
        public frmLineofBusiness()
        {
            InitializeComponent();
            
            proc.ProcessCompleted += new EventHandler(proc_ProcessCompleted);
            btnaddbusiness.Click += new EventHandler(btnaddbusiness_Click);
            
            txtbusinesssearch.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtbusinesssearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
            
          //auto.AddRange(new string[] { "Select 1", "Select 2" });
        }

        void btnaddbusiness_Click(object sender, EventArgs e)
        {
            string cd, bns;
            cd = txtcode.Text;
            bns = txtbusinesssearch.Text;
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            //Instantiate the script object and specify a name for the script.
            script = new CodeScript("INSERTBUSINESS");

            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            //Modified the settings a bit, paki balik na lang po.
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            script.Add(CodeIns.DBU_INSERT, "tbllineofbusiness", new string[] { "fCode", "fDescription" }, new object[] {"'"+cd.Trim()+"'","'"+bns.Trim()+"'"}); 
            //Modified  your query a bit.
            script.Add(CodeIns.DBU_QUERY, "Select  code, description from tbllineofbusiness");

            //All commands are executed here, however the results will be fetched from
            //the ProcessCompleted event. :)

            script.Add(CodeIns.DBU_END);

            //After Contructing the script, pass it to the processor.
            proc.Run(script);
            txtbusinesssearch.Text = "";
            txtbusinesssearch.Focus();
        }

        void proc_ProcessCompleted(object sender, EventArgs e)
        {
            //Make use of the SafeCall method ot safely obtain result from the codeprocessor
            //As it is run from a separate thread.
            proc.SafeCall(this, delegate
            {
                //Check for errors, if there are errors, show it and return to calling code.
                if (proc.State == ProcessorFlag.ReadyErr ||
                    proc.State == ProcessorFlag.DataRdyErr ||
                    proc.State == ProcessorFlag.Error)
                {
                    MessageBox.Show(proc.GetProcessorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //If there are no errors,
                //Check for the Script's name
                switch (proc.GetLastProcessName())
                {
                    case "INSERTBUSINESS":
                    case "POPULATETABLE":
                        //Fetch the data.
                        proc.DataFetch(out dat);
                        //Set the active page to export.
                        dat.SetActivePage(0);
                        //Show the queryu result to the DataGridView
                        x = dat.GetRecordCount(0);
                        blist = new string[x];
                        blist = dat.GetStringList("fdescription");
                        auto.AddRange(blist);
                        txtbusinesssearch.AutoCompleteCustomSource  =  auto;
                        break;
                }
            });
        }

        private void frmLineofBusiness_Load(object sender, EventArgs e)
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            string business;
            business = txtbusinesssearch.Text;
            //Instantiate the script object and specify a name for the script.
            script = new CodeScript("POPULATETABLE");

            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            //Modified the settings a bit, paki balik na lang po.
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));

            //Modified  your query a bit.
            script.Add(CodeIns.DBU_QUERY, "Select  fCode, fDescription from tbllineofbusiness");

            //All commands are executed here, however the results will be fetched from
            //the ProcessCompleted event. :)

            script.Add(CodeIns.DBU_END);

            //After Contructing the script, pass it to the processor.
            proc.Run(script);
        }

           
    }
}
